//
//  TrainingGoalSettingController.swift
//  DaRings
//
//  Created by HFY on 2022/11/30.
//

import UIKit
import MYModule
import JXSegmentedView

public protocol GoalSettingResponseDelegate {
     func setGoal(type: TrainingTypeContent, index: Int, goal: Double)
}

open class TrainingGoalSettingController: ViewController {
    
    
    public var delegate: GoalSettingResponseDelegate?
    
   
    public var type: TrainingType = .unknown {
        
        didSet {
            
            reloadData()
            
        }
        
    }
    
    public var models: [TrainingGoalModel] = []
    
    public var selectedModel: TrainingGoalModel?
    
    public let segementedView = JXSegmentedView()
    
    public let dataSource = JXSegmentedTitleDataSource()
    
    public let indicator = JXSegmentedIndicatorBackgroundView()
    
    public let enterButton = Button(type: .custom)
    
   public let container = TrainingGoalSettingContainerController()

    open override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBar()

        
    }
    
    open func setNavigationBar() {
        
        self.navigation.bar.barTintColor = .white
        self.navigation.item.title = "gps_training_goal_setting".language
        
    }
    
    open override func makeUI() {
        super.makeUI()
        
        indicator.commonConfig(indicatorColor: type.tintColor, shadowColor: type.tintColor.withAlphaComponent(0.15))
        segementedView.dataSource = dataSource
        segementedView.indicators = [indicator]
        segementedView.delegate = self
       
        segementedView.listContainer = container
        
        enterButton.setTitle("gps_training_goal_setting_enter".language, for: .normal)
        enterButton.backgroundColor = type.tintColor
        enterButton.cornerRadius = 25.0
        
        view.addSubview(segementedView)
        self.add(container, toView: view)
        view.addSubview(enterButton)
        
        
        let top = UIApplication.shared.statusBarFrame.height + self.navigation.bar.height + 15
        segementedView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(top)
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
        }
        
        container.view.snp.makeConstraints { make in
            make.top.equalTo(segementedView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(enterButton.snp.top)
        }
        
        enterButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(50)
            make.right.equalToSuperview().offset(-50)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-90)
        }
        
        
    }
    
    open func reloadData() {
        
        models = type.contents.filter({ $0.goalAble == true }).map({ TrainingGoalModel(contentType: $0, selectedTintColor: type.tintColor) })
        
        selectedModel = models[container.defaultSelectedIndex]
        
        let titles = models.compactMap { $0.title }
        
        dataSource.commonConfig(titles: titles, itemWidth: -1, titleNormalFont: Fonts.systemFont(ofSize: 15), titleSelectedColor: UIColor.assist_FFFFFF, titleNormalColor: UIColor.assist_A9A9A9)
        dataSource.isTitleMaskEnabled = true
        
        let inset = JXSegmentedViewAutomaticDimension
        
        segementedView.commonConfig(backgroundColor: UIColor.assist_FFFFFF, inset: inset)
        
        container.models = models
        segementedView.reloadData()
        
    }
    
    open override func bindViewModel() {
        super.bindViewModel()
        
        
        enterButton.rx.tap.subscribe(onNext: {[weak self] in
            
            guard let model = self?.selectedModel, let selectedItem = model.selectedItem, let index = model.items.firstIndex(of: selectedItem) else { return }
            
            self?.navigationController?.popViewController(animated: true)
            
            
            self?.delegate?.setGoal(type: selectedItem.contentType, index: index, goal: selectedItem.target)
            
        }).disposed(by: rx.disposeBag)
        
    }

}

extension TrainingGoalSettingController: JXSegmentedViewDelegate {
    
    public func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {

        selectedModel = models[index]

    }
}



